<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Components\Tools;
use App\Components\DyToools;
use App\Models\DyModels\Douyin;
use App\Models\VoidsModels\Voids;

class TestController extends Controller
{
    function __construct(Request $request)
    {
        $this->parames = $request->all();
        $this->url = Tools::getOneconfig("/config/third.json", array('qiniu', 'qnUrl'));
    }


    //审核操作异步进行
    function saveVideo()
    {
        $voids = new Voids();
        $data = file_get_contents("php://input");
        $data = json_decode($data, true);
        // echo "<pre/>";print_r($data);exit;
        $kid = isset($data['kid']) ? intval($data['kid']) : 0;
        $name = isset($data['name']) ? $data['name'] : '';

        //查询该视频需上传的信息
        $videoData = $voids->selectVideo($kid);
        !$videoData ? exit('视频不存在') : ($videoData['status'] == 0 || $videoData['status'] == 3 ? '' : exit('视频不存在'));

        //查询该视频用户的授权信息
        $token = $voids->selectToken($videoData['open_id']);
        if (!$token) {
            $this->upVideo($kid, $name);
            exit('账号未授权或授权已过期');
        }

        //上传封面
        $img_id = $this->upImgToDY($kid, $videoData['cover'], $videoData['open_id'], $name);

        //上传视频
        $video_id = $this->videoUpToDY($kid, $videoData['share_url'], $token, $name);

        //发布视频
        $video_title = $videoData['title'] ." ". $videoData['talk'];
        $this->videoFbToDY($kid, $token, $video_id, $img_id, $video_title, $name);
    }


    //上传视频封面到抖音 		kid		视频主键ID
    function upImgToDY($kid, $imgUrl, $open_id, $operator)
    {
        $img_id = false;

        $DouyinModel = new Douyin();
        $DyToools = new DyToools();

        //查询该视频用户的授权信息
        $token = $DouyinModel->oneTokenInfo($open_id);
        if (!$token) {
            $this->upVideo($kid, $operator);
            exit('账号未授权或授权已过期');
        }

        $imageUrl = $this->url . "/" . $imgUrl;
        $first = explode('/', $imageUrl);

        $name = isset($first[1]) ? end($first) : '';
        if (!$name) {
            $this->upVideo($kid, $operator, "图片不存在");
            exit("图片不存在");
        }

        $imgList = $DyToools->imageUpload($token['access_token'], $open_id, $imageUrl, $name);
        // echo "<pre/>";print_r($imgList);exit;
        if (isset($imgList['data']['error_code']) && $imgList['data']['error_code'] == 0) {


            //取出图片地址返回
            $img_id = $imgList['data']['image']['image_id'];


        } elseif (isset($imgList['data']['error_code']) && ($imgList['data']['error_code'] == 10008 || $imgList['data']['error_code'] == 2190008)) {


            // access_token 过期
            $tokenOne = $DyToools->refresh_token($token['refresh_token']);
            if (isset($tokenOne['data']['error_code']) && $tokenOne['data']['error_code'] == 0) {


                //更新数据库的 access_token
                $DouyinModel->upToken($tokenOne['data'], $open_id);
                //重新调用上传图片
                $this->upImgToDY($kid, $imgUrl, $open_id);


            } elseif (isset($tokenOne['data']['error_code']) && $tokenOne['data']['error_code'] == 10010) {


                // refresh_token 过期
                $tokenTwo = $DyToools->renew_refresh_token($tokens['refresh_token']);
                if (isset($tokenTwo['data']['error_code']) && $tokenTwo['data']['error_code'] == 0) {


                    //更新数据库的 refresh_token
                    $DouyinModel->upRef($tokenTwo['data'], $open_id);
                    //重新调用上传图片
                    $this->upImgToDY($kid, $imgUrl, $open_id);


                } else {
                    $DouyinModel->upTokenStatus($open_id);
                    $this->upVideo($kid, $operator, '授权已过期');
                    exit("授权已过期");
                }
            } else {
                $this->upVideo($kid, $operator, '审核失败');
            }
        } else {
            $this->upVideo($kid, $operator, '审核失败');
        }
        return $img_id ? $img_id : exit('上传封面失败！');
    }

    /**上传视频到抖音
     *    $videoUrl  视频源地址        $token    授权信息
     */
    function videoUpToDY($kid, $videoUrl, $token, $operator)
    {
        $video_id = false;

        $DyToools = new DyToools();

        $videoUrl = $this->url . "/" . $videoUrl;
        $first = explode('/', $videoUrl);

        $name = isset($first[1]) ? end($first) : '';
        if (!$name) {
            $this->upVideo($kid, $operator, "视频不存在");
            exit("视频不存在");
        }
        $voidList = $DyToools->videoUpload($token['access_token'], $token['open_id'], $videoUrl, $name);
        if (isset($voidList['data']['error_code']) && $voidList['data']['error_code'] == 0) {
            $video_id = $voidList['data']['video']['video_id'];
        } else {
            $this->upVideo($kid, $operator, '审核失败');
        }
        return $video_id ? $video_id : exit('上传视频失败！');
    }

    /**发布视频修改视频信息
     *    $kid  本库的视频id        $token  授权信息        $video_id  上传抖音的视频ID
     *    $img_id  上传抖音的封面ID            $video_title  视频标题+话题
     */
    function videoFbToDY($kid, $token, $video_id, $img_id, $video_title, $operator)
    {
        $result = false;
        $voids = new Voids();
        $DyToools = new DyToools();
        $re = $DyToools->videoCreate($token['access_token'], $token['open_id'], $video_id, $img_id, $video_title);
        if (isset($re['extra']['error_code']) && $re['extra']['error_code'] == 0) {
            //发布成功 修改视频信息 等待抖音审核
            $data = array(
                'item_id' => $re['data']['item_id'],
                'status' => 1,
                'create_time' => date('Y-m-d H:i:s', time()),
                'operator' => $operator,
            );
            $result = $voids->updateAll($kid, $data);
        } else {
            $this->upVideo($kid, $operator, '审核失败');
        }
        $result ? exit('提交抖音审核成功') : exit('提交抖音审核失败');
    }

    //修改视频信息 默认修改-1
    function upVideo($kid, $operator = "", $remark = "")
    {
        $voids = new Voids();
        $remark = $remark ? $remark : "账号未授权或授权已过期";
        $voids->updateAll($kid, ['status' => -1, 'operator' => $operator, 'remark' => $remark]);
    }
}
